package com.masterclient.app.businessmodule.minemodule.redpack.contract;

import android.widget.Toast;

import com.eoner.baselib.enums.FCPageViewState;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.eoner.baselib.container.CommonBaseResponse;
import com.masterclient.app.modules.coupon.beans.RedPackedDetailsBean;
import com.eoner.baselibrary.utils.Utils;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
* Created by TMVPHelper on 2017/08/07
*/
public class RedPackedDetailPresenter extends RedPackedDetailContract.Presenter{

    @Override
    public void getRedPackedOrderDetail(String trade_no, int page) {
        mView.baseShowPageLoading(false);
        Observable<CommonBaseResponse<RedPackedDetailsBean>> observable = RetrofitUtil.getInstance().getRedPackedDetails(trade_no, page == 0?"":(page+""),"15");
        Disposable disposable = toSubscribe(observable, new Consumer<CommonBaseResponse<RedPackedDetailsBean>>() {
            @Override
            public void accept(@NonNull CommonBaseResponse<RedPackedDetailsBean> bean) throws Exception {
                mView.baseHiddenPageLoading();
                mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NORMAL);
                if (bean.getCode() == 0) {
                    mView.getDetailSuccess(bean.getData());
                } else {
                    mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NET_ERROR,bean.getMsg());
                    mView.getDetailFailed();
                    Toast.makeText(Utils.getContext(), bean.getMsg(), Toast.LENGTH_SHORT).show();
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable o) throws Exception {
                mView.baseHiddenPageLoading();
                mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NET_ERROR);
                mView.getDetailFailed();
                o.printStackTrace();
            }
        });
        mRxManager.add(disposable);
    }


}